from Apps.UpdateConfigApp import openUpdateConfig
from Apps.UpdateVersionApp import openUpdateVersion
from Apps.BuildGameApp import openBuildGame
from Apps.Functions.ArchiveGameBuild import ArchiveBuild 
from Apps.GitPublishCommitApp import openGitPublishCommit
from Apps.Functions.PublishGitRelease import PublishRelease

from tkinter import *

import git
import configparser
import requests
import os

config = configparser.ConfigParser()

config.read('../Config/config.ini')
current_version_file = open("../Config/Version.ini", "r")
current_updt_version_file = open("Config/Version.ini", "r")
current_updt_version_code_file = open("Config/VersionCode.ini", "r")
repo = git.Repo("../.git")

current_version = current_version_file.read()
current_updt_version = current_updt_version_file.read()
current_updt_version_code = current_updt_version_code_file.read()

project_name = config.get('ProjectInfo', 'project_name')
git_Username = config.get('UserInfo', 'git_Username')

latest_version_from_git = requests.get(f'https://api.github.com/repos/{git_Username}/{project_name}/releases/latest')

root = Tk()
root.title("Unreal Project Developer Tools ( UPDT )")
root.geometry("450x650")
root.wm_minsize(450, 650)
root.wm_maxsize(450, 650)

frame = Frame(root, padx=10, pady=10)
frame.pack(expand=True)

if os.path.exists('../Config/config.ini'):
    # Update config elements
    UpdateConfig_label = ttk.Label(frame, text="Update config")
    UpdateConfig_label.grid(row=1, column=1)

    UpdateConfig_btn = ttk.Button(frame, text="Update", width=50, command=openUpdateConfig)
    UpdateConfig_btn.grid(row=2, column=1)

    # Update version elements

    UpdateVersion_label = ttk.Label(frame, text="Update version")
    UpdateVersion_label.grid(row=3, column=1)

    UpdateVersion_btn = ttk.Button(frame, text="Update", width=50, command=openUpdateVersion)
    UpdateVersion_btn.grid(row=4, column=1)

    # Build game elements

    BuildGame_label = ttk.Label(frame, text="Build game")
    BuildGame_label.grid(row=5, column=1)

    BuildGame_btn = ttk.Button(frame, text="Build", width=50, command=openBuildGame)
    BuildGame_btn.grid(row=6, column=1)

    # Archive game elements

    ArchiveGame_label = ttk.Label(frame, text="Archive game")
    ArchiveGame_label.grid(row=7, column=1)

    ArchiveGame_btn = ttk.Button(frame, text="Archive", width=50, command=lambda:ArchiveBuild)
    ArchiveGame_btn.grid(row=8, column=1)

    if os.path.isdir(f'../Builds/{current_version}'):
        ArchiveGame_btn.state(['!disabled'])
    else:
        ArchiveGame_btn.state(['disabled'])

    # Publish qit commit elements

    Git_Commit_label = ttk.Label(frame, text="Publish git commit")
    Git_Commit_label.grid(row=9, column=1)

    Git_Commit_btn = ttk.Button(frame, text="Publish", width=50, command=openGitPublishCommit)
    Git_Commit_btn.grid(row=10, column=1)

    if repo.is_dirty:
        Git_Commit_btn.state(['!disabled'])
    else:
        Git_Commit_btn.state(['disabled'])

    # Publish qit release elements
    Git_Release_label = ttk.Label(frame, text="Publish git Release")
    Git_Release_label.grid(row=11, column=1)

    Git_Release_btn = ttk.Button(frame, text="Publish", width=50, command=lambda:PublishRelease)
    Git_Release_btn.grid(row=12, column=1)

    if latest_version_from_git != current_version:
        Git_Release_btn.state(['disabled'])
    else:
        Git_Release_btn.state(['!disabled'])

    # Project & app versions & Credits
    Versions_Frame = ttk.Frame(frame)
    Versions_Frame.grid(row=13, column=1)

    Project_Version_label = ttk.Label(Versions_Frame, text=f'{project_name} version: {current_version} |')
    Project_Version_label.grid(row=1, column=1)

    UPDT_Version = ttk.Label(Versions_Frame, text=f'UPDT version: {current_updt_version} ({current_updt_version_code})')
    UPDT_Version.grid(row=1, column=2)

    Credits_label = ttk.Label(frame, text="By cherrymerry❤️")
    Credits_label.grid(row=14, column=1)
else:
    # Generate config elements
    GenerateConfig_label = ttk.Label(frame, text="Generate config")
    GenerateConfig_label.grid(row=1, column=1)

    GenerateConfig_Button = ttk.Button(frame, width=40, text="Generate", command=openUpdateConfig)
    GenerateConfig_Button.grid(row=2, column=1)

root.mainloop()
